package com.eightbears.beboy.base.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

import com.eightbears.beboy.R;


/**
 * @author 公用对话框
 */
public class AddCodeDialog extends Dialog {

    public AddCodeDialog(Context context) {
        super(context);
    }

    public AddCodeDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private Context context;
        private String title;
        private Bitmap bitmap;
        private String tv_price;
        private String tv_code;


        private String order_sure;
        private String order_cancel;
        private View contentView;
        private OnClickListener positiveButtonClickListener;
        private OnClickListener negativeButtonClickListener;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setTv_code(String tv_code) {
            this.tv_code = tv_code;
            return this;
        }

        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        public Builder setTv_price(String tv_price) {
            this.tv_price = tv_price;
            return this;
        }

        /**
         * 设置二维码图片
         *
         * @param bitmap
         * @return
         */
        public Builder setBitmap(Bitmap bitmap) {
            this.bitmap = bitmap;
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        public Builder setPositiveButton(int order_sure,
                                         OnClickListener listener) {
            this.order_sure = (String) context
                    .getText(order_sure);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String order_sure,
                                         OnClickListener listener) {
            this.order_sure = order_sure;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int order_cancel,
                                         OnClickListener listener) {
            this.order_cancel = (String) context
                    .getText(order_cancel);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String order_cancel,
                                         OnClickListener listener) {
            this.order_cancel = order_cancel;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public AddCodeDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final AddCodeDialog dialog = new AddCodeDialog(context, R.style.Dialog);
            dialog.setCancelable(true);
            dialog.setCanceledOnTouchOutside(true);
            View layout = inflater.inflate(R.layout.dialog_add_code, null);
            dialog.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

            if (bitmap != null) {
                ((ImageView) layout.findViewById(R.id.tv_content)).setImageBitmap(bitmap);
            } else {
                layout.findViewById(R.id.tv_content).setVisibility(View.GONE);
            }

//            //确定按钮
//            if (order_sure != null) {
//
//                ((TextView) layout.findViewById(R.id.order_sure)).setText(order_sure);
//                if (positiveButtonClickListener != null) {
//                    ((TextView) layout.findViewById(R.id.order_sure))
//                            .setOnClickListener(new View.OnClickListener() {
//                                @Override
//                                public void onClick(View v) {
//                                    positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
//                                }
//                            });
//                }
//            }
//            //取消按钮
//            if (order_cancel != null) {
//                ((TextView) layout.findViewById(R.id.order_cancel)).setText(order_cancel);
//                if (negativeButtonClickListener != null) {
//                    ((TextView) layout.findViewById(R.id.order_cancel))
//                            .setOnClickListener(new View.OnClickListener() {
//                                @Override
//                                public void onClick(View v) {
//                                    negativeButtonClickListener.onClick(dialog,
//                                            DialogInterface.BUTTON_NEGATIVE);
//                                }
//                            });
//                }
//            } else {
//                (layout.findViewById(R.id.layout_cancel)).setVisibility(View.GONE);
//            }
            dialog.setContentView(layout);
            return dialog;
        }

    }
}
